package com.example.myapplication

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.myapplication.service.AppService
import com.example.myapplication.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_image.*
import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Func1
import rx.schedulers.Schedulers

class ImageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image)
button5.setOnClickListener(){
       loadImage()
}
    }
  private  fun loadImage(){
        ServerCreator.create(AppService::class.java) .downloadPic()
            .map(object: Func1<ResponseBody, Bitmap>
            { override fun call(t: ResponseBody?): Bitmap {
                return BitmapFactory.decodeStream(t?.byteStream()) } })
            .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object: Observer<Bitmap> { override fun onCompleted() { //停止pregressBar Log.d("finished","finished.................") }
            }
                override fun onError(e: Throwable?) { Log.d("error","error................") }override fun onNext(t: Bitmap?)
                { imageView.setImageBitmap(t) } }) }

}